﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.ModelConfiguration;
using CSP.Model.CSP;
using System.ComponentModel.DataAnnotations;

namespace CSP.DataAccess.Mapping
{
    public class CreditLineMap : EntityTypeConfiguration<CreditLine>
    {
        public CreditLineMap()
        {
            // Primary Key
            this.HasKey(t => t.id);

            // Properties
            this.Property(t => t.FromUsername)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.ToUserName)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.RatingContent)
                .HasMaxLength(500);

            this.Property(t => t.OrderNumber)
                .HasMaxLength(50);

            // Table & Column Mappings
            this.ToTable("CreditLines");
            this.Property(t => t.id).HasColumnName("id");
            this.Property(t => t.UserCreditId).HasColumnName("UserCreditId");
            this.Property(t => t.FromUsername).HasColumnName("FromUsername");
            this.Property(t => t.ToUserName).HasColumnName("ToUserName");
            this.Property(t => t.ServiceAttitude).HasColumnName("ServiceAttitud");
            this.Property(t => t.ServiceAbility).HasColumnName("ServiceAbility");
            this.Property(t => t.CreditRating).HasColumnName("CreditRating");
            this.Property(t => t.RatingContent).HasColumnName("RatingContent");
            this.Property(t => t.RatingType).HasColumnName("RatingType");
            this.Property(t => t.RatingTime).HasColumnName("RatingTime");
            this.Property(t => t.ServiceInfoId).HasColumnName("ServiceInfoId");
            this.Property(t => t.OrderNumber).HasColumnName("OrderNumber");

            // Relationships
            this.HasOptional(t => t.Order)
                .WithMany(t => t.CreditLines)
                .HasForeignKey(d => d.OrderNumber);
            this.HasRequired(t => t.ServiceInfo)
                .WithMany(t => t.CreditLines)
                .HasForeignKey(d => d.ServiceInfoId);
            this.HasRequired(t => t.UserCredit)
                .WithMany(t => t.CreditLines)
                .HasForeignKey(d => d.UserCreditId);

        }
    }
}
